package com.furschina.model;

import java.util.Arrays;

import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OrderColumn;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

import org.apache.commons.codec.digest.DigestUtils;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.Index;
import org.hibernate.validator.constraints.Email;

@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@org.hibernate.annotations.Entity(dynamicInsert = true, dynamicUpdate = true)
public class User extends BaseModel {
	/**
	 * 
	 */
	private static final long serialVersionUID = -2683868977642708022L;
	@Column(unique = true, updatable = false)
	@Size(max = 20, min = 4)
	@NotNull
	@Pattern(regexp = "^[a-zA-Z][a-zA-Z0-9-_]+$")
	@Index(name = "idx_uname")
	private String username;
	@Column(unique = true, nullable = false, updatable = false)
	@Email
	@NotNull
	@Index(name = "idx_email")
	private String email;
	@NotNull
	@Size(max = 40, min = 8)
	private String password;
	private boolean enabled = true;
	private String avatar;
	@ElementCollection(fetch = FetchType.EAGER)
	@OrderColumn(name = "idx")
	@Cascade(CascadeType.ALL)
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	private String[] authorities = { "ROLE_USER" };
	private String verification;
	private boolean verified = false;

	/**
	 * 
	 */
	public User() {
		super();
	}

	/**
	 * 
	 */
	public User(Long id) {
		super();
		this.id = id;
	}

	/**
	 * @return the avatar
	 */
	public String getAvatar() {
		return avatar;
	}

	/**
	 * @param avatar
	 *            the avatar to set
	 */
	public void setAvatar(String avatar) {
		this.avatar = avatar;
	}

	/**
	 * @return the verified
	 */
	public boolean isVerified() {
		return verified;
	}

	/**
	 * @param verified
	 *            the verified to set
	 */
	public void setVerified(boolean verified) {
		this.verified = verified;
	}

	/**
	 * @return the authorities
	 */
	public String[] getAuthorities() {
		return authorities;
	}

	/**
	 * @param authorities
	 *            the authorities to set
	 */
	public void setAuthorities(String[] authorities) {
		this.authorities = authorities;
	}

	/**
	 * @return the verification
	 */
	public String getVerification() {
		return verification;
	}

	/**
	 * @param verification
	 *            the verification to set
	 */
	public void setVerification(String activition) {
		this.verification = activition;
	}

	/**
	 * @return the enabled
	 */
	public boolean isEnabled() {
		return enabled;
	}

	/**
	 * @param enabled
	 *            the enabled to set
	 */
	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String nickName) {
		this.username = nickName;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void genVerification() {
		this.verification = DigestUtils.shaHex(this.toString());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "User [username=" + username + ", email=" + email + ", password=" + password
				+ ", enabled=" + enabled + ", authorities=" + Arrays.toString(authorities) + "]";
	}

}
